<%@ page contentType="text/html;charset=UTF-8" language="java" import="java.sql.*" %>
<%request.setCharacterEncoding("utf-8");%>
<%String id = request.getParameter("id");%>
<%String student_name = request.getParameter("name");%>
<%String birthday = request.getParameter("birthday");%>
<%String classID = request.getParameter("class");%>
<html>
<head>
    <title>Title</title>
    <%!
        ResultSet rs;//数据库查询结果集对象
    %>
    <%
        String dbName = "jdbc";//数据库名称
        String userName ="root";//账号
        String dbPassword = "123456";//密码
        String url = "jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+dbPassword;
        Class.forName("com.mysql.cj.jdbc.Driver");//加载数据库驱动
        Connection conn = DriverManager.getConnection(url);//连接数据库
        Statement statement=conn.createStatement();//数据库
        String sql = "INSERT into student(id,student_name,birthday,classID) VALUES('"+id+"','"+student_name+"','"+birthday+"','"+classID+"')";//查询 student表的sql命令
        int result = statement.executeUpdate(sql);//执行插入操作
        if(result>0)
            out.print(" <script type='text/javascript'>alert('插入成功！');</scipt> ");
        else
            out.print(" <script type='text/javascript'>alert('插入失败！');</scipt> ");
        sql = "SELECT * FROM student";//查询 student表的sql命令
        rs = statement.executeQuery(sql);//执行查询操作

    %>
</head>
<body>
 <table border="1">
    <th colspan="4" style="font-size: x-large; alignment:center";>学生表信息</th>
    <tr style="text-align: center">
        <td>学生学号：</td>
        <td>学生姓名:</td>
        <td>出生日期:</td>
        <td>所在班级:</td>
    </tr>
    <%-- 循环查询结果集    --%>
    <% while (rs.next()){ %>
    <%-- 使用表达式显示结果集中对应的列值        --%>
    <tr style="text-align: center">
        <td><%=rs.getString("id") %></td>
        <td><%=rs.getString("stuname")%></td>
        <td><%=rs.getString("birthday")%></td>
        <td><%= rs.getString("stuclass")%></td>
    </tr>
    <% } %>
    <%--   查询结束 关闭结果集，student对象和数据库连接    --%>
    <%
        rs.close();
        statement.close();
        conn.close();
    %>
</table>
</body>
</html>
